<template>
	<div class="system-role-dialog-container energy-from energy-dialog">
		<el-dialog center align-center v-model="state.dialog.isShowDialog" width="30%" @close="resetData">
			<template #header="{ titleClass }">
				<div :class="titleClass">修改套餐信息</div>
			</template>
			<el-form ref="roleDialogFormRef" :model="state.ruleForm" :rules="state.rules" size="default" label-width="100px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="套餐包名" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入租户名" clearable disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="到期时间" prop="expireTime">
							<el-date-picker
								class="w100"
								v-model="state.ruleForm.expireTime"
								type="date"
								format="YYYY-MM-DD HH:mm:ss"
								value-format="YYYY-MM-DD HH:mm:ss"
								:default-time="new Date('1 00:00:00')"
								placeholder="请选择到期时间"
							/>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="使用状态" prop="status">
							<el-switch
								v-model="state.ruleForm.status"
								size="default"
								:active-value="0"
								:inactive-value="1"
								active-text="启用"
								inactive-text="禁用"
								inline-prompt
								style="--el-switch-on-color: #13ce66"
							>
							</el-switch>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<div class="flex row-center mt10 mb10">
				<div class="btn3" @click="onCancel">
					<el-icon> <ele-Close /> </el-icon><span class="ml6">取 消</span>
				</div>
				<div class="btn1 ml10" @click="onSubmit">
					<el-icon> <ele-Check /> </el-icon><span class="ml6">保 存</span>
				</div>
			</div>
		</el-dialog>
	</div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { usePackageApi } from '/@/api/system/package/index';
import { ElNotification } from 'element-plus';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
// 定义变量内容
const roleDialogFormRef = ref();
const state: any = reactive({
	ruleForm: {
		tenantPackageId: '',
		name: '', // 套餐包名
		expireTime: '', // 到期时间,
		status: '', // 使用状态
	},
	dialog: {
		isShowDialog: false,
	},
	rules: {
		expireTime: [{ required: true, message: '过期时间不能为空', trigger: 'blur' }],
		status: [{ required: true, message: '请选择套餐包使用状态', trigger: 'blur' }],
	},
});

// 打开弹窗
const openDialog = (row: any) => {
	state.ruleForm = {
		tenantPackageId: row.tenantPackageId,
		name: row.name, // 套餐包名
		expireTime: row.expireTime, // 到期时间,
		status: row.status, // 使用状态
	};
	state.dialog.isShowDialog = true;
};

// 修改套餐包
const handleEditTenant = async () => {
	delete state.ruleForm.name;
	const data = await usePackageApi().updateTenantPackage(state.ruleForm);
	if (data.code == 200) {
		emit('refresh');
		ElNotification({
			title: '提示',
			message: '修改套餐包信息成功',
			position: 'top-right',
			type: 'success',
		});
	}
};

// 提交
const onSubmit = () => {
	roleDialogFormRef.value.validate((valid: any) => {
		if (valid) {
			handleEditTenant();
		} else {
			ElNotification({
				title: '提示',
				message: '校验失败,请填写完整套餐包信息',
				position: 'top-right',
				type: 'error',
			});
		}
		closeDialog();
	});
};

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
	closeDialog();
};

// 表单清空
const resetData = () => {
	roleDialogFormRef.value.resetFields();
	state.ruleForm = {
		name: '', // 套餐包名
		expireTime: '', // 到期时间,
		status: '', // 使用状态
		tenantPackageId: '',
	};
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
